package com.tan.lbs.service.impl;

import java.util.ArrayList;
import java.util.Collections;

import com.tan.lbs.dao.ItemDAO;
import com.tan.lbs.dao.LocationDAO;
import com.tan.lbs.dao.MediaDAO;
import com.tan.lbs.dao.QuestDAO;
import com.tan.lbs.dao.impl.ItemDAOImpl;
import com.tan.lbs.dao.impl.LocationDAOImpl;
import com.tan.lbs.dao.impl.MediaDAOImpl;
import com.tan.lbs.dao.impl.QuestDAOImpl;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.ItemHeader;
import com.tan.lbs.entity.Location;
import com.tan.lbs.entity.Quest;
import com.tan.lbs.entity.QuestHeader;
import com.tan.lbs.service.QuestService;
import com.tan.lbs.util.QuestHeaderComparator;

public class QuestServiceImpl implements QuestService {
	
	private QuestDAO questDAO;
	private LocationDAO locationDAO;
	private MediaDAO mediaDAO;
	
	public QuestServiceImpl() {
		questDAO = new QuestDAOImpl();
		locationDAO = new LocationDAOImpl();
		mediaDAO = new MediaDAOImpl();
	}

	@Override
	public String createQuestWithoutLocation(Quest quest) {
		return questDAO.create(quest); 
	}

	@Override
	public void modifyQuestWithoutLocation(Quest quest) {
		Quest oldQuest  = questDAO.readByID(quest.getQuestID());
		quest.setLocationID(oldQuest.getLocationID());
		quest.setIconID(oldQuest.getIconID());
		quest.setQuestOrder(oldQuest.getQuestOrder());
		questDAO.update(quest);
	}

	@Override
	public Quest readQuestDetails(String questID) {
		return questDAO.readByID(questID);
	}

	@Override
	public String createLocation(Location location, String questID) {
		Quest quest = questDAO.readByID(questID);
		String locationID = locationDAO.create(location);
		quest.setLocationID(locationID);
		questDAO.update(quest);
		return locationID;
	}

	@Override
	public ArrayList<QuestHeader> retrieveQuestList(String gameID) {
		ArrayList<Quest> quests = questDAO.searchByGame(gameID);
		ArrayList<QuestHeader> headers = new ArrayList<QuestHeader>();
		for(Quest quest : quests) {
			QuestHeader header = new QuestHeader();
			if(quest.getLocationID()!=null) {
				header.setLocation(locationDAO.readByID(quest.getLocationID()));
			}
			header.setQuestID(quest.getQuestID());
			header.setQuestTitle(quest.getTitle());
			header.setQuestOrder(quest.getQuestOrder());
			headers.add(header);
		}
		Collections.sort(headers,new QuestHeaderComparator());
		return headers;
	}

	@Override
	public void deleteLocation(String questID, String locationID) {
		Quest quest = questDAO.readByID(questID);
		quest.setLocationID(null);
		questDAO.update(quest);
		locationDAO.delete(locationID);

	}

	@Override
	public void deleteQuestAndLocation(String questID) {
		String locationID = questDAO.readByID(questID).getLocationID();
		String iconID = questDAO.readByID(questID).getIconID();
		questDAO.delete(questID);
		locationDAO.delete(locationID);
		mediaDAO.delete(iconID);
	}

	@Override
	public void modifyQuestOrderForGame(ArrayList<String> questIDs) {
		for(int i = 0 ;i < questIDs.size();++i) {
			Quest quest  = questDAO.readByID(questIDs.get(i));
			quest.setQuestOrder(i+1);
			questDAO.update(quest);
		}
		
	}

}
